package com.zhiyou.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhiyou.dao.ConDao;
import com.zhiyou.model.Contract;
import com.zhiyou.util.DBUtils;
import com.zhiyou.util.PageBean;

public class ConDaoImpl implements ConDao {

	@Override
	public PageBean<Contract> findAll(int pc, int ps) {
		PageBean<Contract> pb = new PageBean<>();
		DBUtils db = new DBUtils();
		List<Contract> list = new ArrayList<>();
		pb.setPc(pc);
		pb.setPs(ps);
		int tr = 0;
		String sq = "select count(*) from contract " ;
		Object ob[] = {};
		ResultSet r = db.select(sq, ob);
		if(r == null){
			pb.setTr(tr);
		}else{
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		String sql = "select * from contract limit ?,?" ;
		Object obj[] = {(pc-1)*ps,ps };
		ResultSet rs = db.select(sql, obj);
		if(rs == null){
		}else{
			try{
				while(rs.next()){
					Contract con = new Contract(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getDate(4), rs.getDate(5), rs.getDate(6), rs.getLong(7), rs.getInt(8), rs.getDouble(9), rs.getInt(10), rs.getString(11), rs.getInt(12));
					list.add(con);
				}
			}catch(SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		pb.setBeanList(list);
		db.close();
		return pb;
	}

	@Override
	public Contract findById(int id) {
		String sql = "select * from contract where lc_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		ResultSet rs =db.select(sql, obj);
		if(rs == null){
			db.close();
			return null;
		}else{
			try {
				while(rs.next()){
					Contract con = new Contract(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getDate(4), rs.getDate(5), rs.getDate(6), rs.getLong(7), rs.getInt(8), rs.getDouble(9), rs.getInt(10), rs.getString(11), rs.getInt(12));
					db.close();
					return con;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			db.close();
			return null;
		}
	}

	@Override
	public boolean delete(int id) {
		String sql = "update contract set lc_contract_state = 5 where lc_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(Contract contract) {
		String sql = "update contract set flat_id=?,tenant_id = ?,lc_contract_date = ?,lc_contract_start_date = ?,lc_contract_end_date = ?,lc_total_rent = ?,lc_pay_way = ?,lc_deposit = ?,lc_period_payment = ?,lc_contract_signer = ?,lc_contract_state = ? where lc_id = ?";
		Object obj[] = {contract.getFlat_id(),contract.getTenant_id(),contract.getLc_contract_date(),contract.getLc_contract_start_date(),contract.getLc_contract_end_date(),contract.getLc_total_rent(),contract.getLc_pay_way(),contract.getLc_deposit(),contract.getLc_period_payment(),contract.getLc_contract_signer(),contract.getLc_contract_state(),contract.getLc_id()};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean add(Contract contract) {
		String sql = "insert into contract values(null,?,?,?,?,?,?,?,?,?,?,?) ";
		Object obj[] = {contract.getFlat_id(),contract.getTenant_id(),contract.getLc_contract_date(),contract.getLc_contract_start_date(),contract.getLc_contract_end_date(),contract.getLc_total_rent(),contract.getLc_pay_way(),contract.getLc_deposit(),contract.getLc_period_payment(),contract.getLc_contract_signer(),contract.getLc_contract_state()};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int[] dels(int[] id) {
		String sql = "update contract set lc_contract_state = 5 where lc_id = ?";
		DBUtils db = new DBUtils();
		int nums[] = db.dels(sql, id);
		db.close();
		return nums;
	}

	@Override
	public PageBean<Contract> find(String way, String state, String name, int pc, int ps) {
		DBUtils db = new DBUtils();
		PageBean<Contract> pb = new PageBean<>();
		pb.setPc(pc);
		pb.setPs(ps);
		StringBuffer sq = new StringBuffer("select count(*) from contract where 1=1 ");
		List<Object> list = new ArrayList<>();
		if(!way.equals("-1")){
			sq.append(" and lc_pay_way = ? ");
			list.add(way);
		}
		if(!state.equals("-1")){
			sq.append(" and lc_contract_state = ? ");
			list.add(Integer.parseInt(state));
		}
		if(name!=null&&!name.trim().isEmpty()){
			sq.append(" and lc_contract_signer like ? ");
			list.add("%"+name+"%");
		}
		int tr = 0;
		ResultSet r = db.select(sq.toString(), list.toArray());
		if(r==null){
			pb.setTr(0);
		}else{ 
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		StringBuffer sql = new StringBuffer(sq.toString().replace("count(*)", "*"));
		sql.append(" limit ?,?");
		list.add((pc-1)*ps);
		list.add(ps);
		List<Contract> list1 = new ArrayList<>();
		ResultSet rs = db.select(sql.toString(), list.toArray());
		if (rs==null) {
		}else{
			try {
				while(rs.next()){
					Contract con = new Contract(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getDate(4), rs.getDate(5), rs.getDate(6), rs.getLong(7), rs.getInt(8), rs.getDouble(9), rs.getInt(10), rs.getString(11), rs.getInt(12));
					list1.add(con);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list1);
		db.close();
		return pb;
		}
	
	
}
